<?php

namespace Biz\service\springHomeDecActivity;

use Biz\model\springHomeDecActivity\Wish as mWish;
use Biz\pockets\constInterface\SpringHomeDecConfig;
use Doraemon\pockets\constInterface\LinkConf;
use Doraemon\tools\Hhzurl;
use Doraemon\tools\LinkFormat;
use Doraemon\pockets\constInterface\Push\PushId;
use Doraemon\service\Push\PushService;
use Doraemon\tools\Log;
use Medoo\Medoo;

class RemindService implements SpringHomeDecConfig
{
    const QUERY_PER_PAGE_COUNT = 500;

    /**
     * 抽奖提醒
     * @param $pageCount
     * @return bool
     * @throws \Exception
     */
    public static function lotteryRemind($pageCount = 0)
    {
        $where['lottery_remind'] = mWish::LOTTERY_REMIND_YES;
        $where['data_status'] = mWish::STATUS_NORMAL;
        $pageCount = $pageCount > 0 ? $pageCount : self::QUERY_PER_PAGE_COUNT;
        $ret = mWish::dataChunk($where, $pageCount, function ($rows) {
            foreach ($rows as $row) {
                $res = self::pushMsgToUser($row['uid']);
                if (!$res) {
                    Log::error('lottery_remind_fail', $row);
                }
            }
            return true;
        });
        return $ret;
    }

    /**
     * 给用户推送抽奖提醒消息
     * @param $uid
     * @param $title
     * @return bool
     * @throws \Exception
     */
    public static function pushMsgToUser($uid, $title = '')
    {
        $link = Hhzurl::getDomain() . self::REMIND_LINK_URL;
        $linkArr = LinkFormat::generateLink(LinkConf::LINK_APP_FULLSCREEN, json_encode(['url' => $link]));
        $linkUrl = $linkArr['app_template'];
        $pushId = PushId::SPRING_HOME_DEC . $uid . '_' . date('Y-m-d');
        $title = empty($title) ? self::getPushTitle() : $title;
        $params = [
            'uid' => $uid,
            'title' => self::ACTIVITY_REMIND_ALERT,
            'alert' => $title,
            'param' => [
                'link' => $linkUrl,
                'push_id' => $pushId,
                'image' => ''
            ]
        ];
        $res = PushService::send($params);
        return $res;
    }


    /**
     * 获取推送消息标题
     * @return string
     */
    private static function getPushTitle()
    {
        if (time() >= strtotime(self::REMIND_CHANGE_DATE)) {
            return self::ACTIVITY_REMIND_TITLES[1];
        }
        return self::ACTIVITY_REMIND_TITLES[0];
    }


    /**
     * 新抽奖消息提醒
     * @param int $modCount
     * @param int $modValue
     * @return bool
     */
    public static function newLotteryRemind(int $modCount, int $modValue)
    {
        $where = Medoo::raw("where id & :mod_count = :mod_value and lottery_remind = :lottery_remind and data_status = :data_status order by id asc limit :limit_count ",
            [
                ':mod_count' => $modCount - 1,
                ':mod_value' => $modValue,
                ':lottery_remind' => mWish::LOTTERY_REMIND_YES,
                ':data_status' => mWish::STATUS_NORMAL,
                ':limit_count' => self::QUERY_PER_PAGE_COUNT,
            ]
        );
        $fields = ['id', 'uid', 'lottery_remind'];
        $results = mWish::select($fields, $where);
        if (!($results && is_array($results))) {
            return false;
        }
        while (count($results) > 0) {

            foreach ($results as $result) {
                self::pushMsgToUser($result['uid']);
            }

            $lastRecord = array_pop($results);

            $where = Medoo::raw("where id > :last_max_id and id & :mod_count = :mod_value and lottery_remind = :lottery_remind and data_status = :data_status order by id asc limit :limit_count ",
                [
                    ':mod_count' => $modCount - 1,
                    ':mod_value' => $modValue,
                    ':lottery_remind' => mWish::LOTTERY_REMIND_YES,
                    ':data_status' => mWish::STATUS_NORMAL,
                    ':limit_count' => self::QUERY_PER_PAGE_COUNT,
                    ':last_max_id' => $lastRecord['id'],
                ]
            );

            $results = mWish::select($fields, $where);

            if (!($results && is_array($results))) {
                break;
            }
        }

        return true;

    }


}